package demo.anno;

import java.lang.reflect.Method;

public class TestMain {
    
    static void test1() throws SecurityException, ClassNotFoundException
    {
        int passed = 0, failed = 0;
        String className = "demo.anno.Foo";
        for (Method m : Class.forName(className).getMethods()) {
            if (m.isAnnotationPresent(Test.class)) { // 如果有Test标记则执行
                try {
                    m.invoke(null);
                    passed++;
                }catch (Throwable ex) {
                    System.out.printf("Test %s failed: %s %n", m, ex.getClass(), failed);
                    System.out.printf("Test %s failed: %s %n", m, ex.getCause(), failed);
                    System.out.println();
                    failed++;
                }
            }
        }
        System.out.printf("Passed: %d, Failed %d%n", passed, failed);
    }
    
    static void test2() throws SecurityException, ClassNotFoundException
    {
        int passed = 0, failed = 0;
        String className = "demo.anno.Foo2";
        for (Method m : Class.forName(className).getMethods()) {
            if (m.isAnnotationPresent(SingleValue.class)) { // 如果有SingleValue标记则执行
                System.out.println(m.getName());
                SingleValue sv = m.getAnnotation(SingleValue.class); // 重点在此
                try {
                    m.invoke(null, sv.value());
                    passed++;
                }catch (Throwable ex) {
                    System.out.printf("Test %s failed: %s %n", m, ex.getClass(), failed);
                    System.out.printf("Test %s failed: %s %n", m, ex.getCause(), failed);
                    System.out.println();
                    failed++;
                }
            }
        }
    }
    public static void main(String... args) throws SecurityException, ClassNotFoundException {
        test1();
        test2();
    } 
}
